from selenium import webdriver
from selenium.webdriver.common.by import By
import time


def send_message(username, message):
    print(f"Sending message to {username}: {message}")


# 设置用户名、密码和内容
username = 'love001'
password = '123456'
content = None

# 创建 WebDriver 实例（假设使用 Chrome）
driver = webdriver.Chrome()

# 打开页面
driver.get("https://kkw-tuiguang-admin.kkwanyx.com/game?code=NDk3MA%3D%3D")
time.sleep(2)

# 点击开始游戏
driver.find_element(By.XPATH, "//*[contains(text(),'开始游戏')]").click()
send_message(username, "点击开始游戏")
time.sleep(2)

# 点击第四个图片元素
driver.find_element(By.CSS_SELECTOR, "img:nth-of-type(4)").click()

# 点击账号密码登录
driver.find_element(By.XPATH, "//*[contains(text(),'账号密码登录')]").click()
send_message(username, "账号密码登录")

# 输入用户名
username_input = driver.find_element(By.CSS_SELECTOR, "input[type='text']:first-of-type")
username_input.click()
username_input.send_keys(username)

# 输入密码
password_input = driver.find_element(By.CSS_SELECTOR, "input[type='text']:nth-of-type(2)")
password_input.click()
password_input.send_keys(password)

# 点击立即登录
driver.find_element(By.XPATH, "//*[contains(text(),'立即登录')][1]").click()
send_message(username, "立即登录")

# 点击进入游戏
driver.find_element(By.XPATH, "//*[contains(text(),'进入游戏')]").click()
send_message(username, "点击 进入游戏")
time.sleep(3)

# 截图
driver.save_screenshot("xx.png")

# 选择服（假设坐标正确）
driver.execute_script(f"window.scrollTo(0, 0);")
driver.execute_script(
    f"var evt = new MouseEvent('click', {{ bubbles: true, cancelable: true, view: window, clientX: 185, clientY: 432 }});document.elementFromPoint(185, 432).dispatchEvent(evt);")

# 关闭浏览器
driver.quit()
